package com.zzdx.deployment.web.controller.project;

import com.zzdx.deployment.common.annotation.Log;
import com.zzdx.deployment.common.base.AjaxResult;
import com.zzdx.deployment.common.enums.BusinessType;
import com.zzdx.deployment.common.page.TableDataInfo;
import com.zzdx.deployment.common.support.Convert;
import com.zzdx.deployment.common.utils.ExcelUtil;
import com.zzdx.deployment.framework.util.ShiroUtils;
import com.zzdx.deployment.framework.web.base.BaseController;
import com.zzdx.deployment.system.domain.Project;
import com.zzdx.deployment.system.domain.ProjectVersion;
import com.zzdx.deployment.system.domain.SysUser;
import com.zzdx.deployment.system.service.IProjectService;
import com.zzdx.deployment.system.service.IProjectVersionService;
import com.zzdx.deployment.web.controller.common.CommonFunction;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 项目 信息操作处理
 * 
 * @author mason
 * @date 2018-12-27
 */
@Controller
@RequestMapping("/project/project")
public class ProjectController extends BaseController
{
    private String prefix = "project/project";
	
	@Autowired
	private IProjectService projectService;

	@Autowired
	private IProjectVersionService projectVersionService;
	
	@RequiresPermissions("project:project:view")
	@GetMapping()
	public String project()
	{
	    return prefix + "/project";
	}
	
	/**
	 * 查询项目列表
	 */
	@RequiresPermissions("project:project:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Project project)
	{
		Long userId = ShiroUtils.getUserId();
		String loginName = ShiroUtils.getLoginName();
		//管理员可查询全部项目，其它只能查看自己创建的项目
		if(!SysUser.isAdmin(userId)){
			if(project == null){
				project = new Project();
			}
			project.setCreateBy(loginName);
		}
		startPage();
        List<Project> list = projectService.selectProjectList(project);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出项目列表
	 */
	@RequiresPermissions("project:project:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Project project)
    {
    	List<Project> list = projectService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        return util.exportExcel(list, "project");
    }
	
	/**
	 * 新增项目
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存项目
	 */
	@RequiresPermissions("project:project:add")
	@Log(title = "项目", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Project project)
	{
		String loginName = ShiroUtils.getLoginName();
		project.setCreateBy(loginName);
		project.setCreateTime(new Date());
		return toAjax(projectService.insertProject(project));
	}

	/**
	 * 修改项目
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		Project project = projectService.selectProjectById(id);
		mmap.put("project", project);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存项目
	 */
	@RequiresPermissions("project:project:edit")
	@Log(title = "项目", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Project project)
	{
		String loginName = ShiroUtils.getLoginName();
		project.setUpdateBy(loginName);
		project.setUpdateTime(new Date());
		return toAjax(projectService.updateProject(project));
	}
	
	/**
	 * 删除项目
	 */
	@RequiresPermissions("project:project:remove")
	@Log(title = "项目", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		//获取项目对应的项目版本
		List<ProjectVersion> allProjectVersions = new ArrayList<>();
		String[] pIds = Convert.toStrArray(ids);
		if(pIds.length > 0){
			for (String pId: pIds) {
				ProjectVersion projectVersion = new ProjectVersion();
				projectVersion.setProjectId(Integer.parseInt(pId));
				List<ProjectVersion> projectVersions = projectVersionService.selectProjectVersionList(projectVersion);
				allProjectVersions.addAll(projectVersions);
			}
		}
		String projectVersionIds = CommonFunction.convertToProjectVersionIdStr(allProjectVersions);
		int updatedRows = projectService.deleteProjectByIds(ids);
		//若项目删除成功，则删除其版本
		if(updatedRows>0){
			projectVersionService.deleteProjectVersionByIds(projectVersionIds);
		}
		return toAjax(updatedRows);
	}

}
